package com.sgcc.fj.pmos.e_fj_home.activity;

import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.fj.pmos.arouter.home.HomeArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.fj.pmos.e_fj_home.adapter.InfomationListAdapter;
import com.sgcc.fj.pmos.e_fj_home.bean.ByPublishTypeBean;
import com.sgcc.fj.pmos.e_fj_home.bean.InfomationListBean;
import com.sgcc.fj.pmos.e_fj_home.contract.InfomationListContract;
import com.sgcc.fj.pmos.e_fj_home.presenter.InfomationListPresenter;

/**
 * 信息披露列表
 */
@Route(path = HomeArouterPath.HOME_INFOMATIONLIST)
public class InfomationListActivity extends MvpBaseRecycleActivity<InfomationListBean.DataBean.ListBean, InfomationListPresenter> implements InfomationListContract.View {

    private InfomationListAdapter mInfomationListAdapter;
    private String mLabel, mInfoCode = "", informationId = "";

    @Override
    protected void receiveIntentExtras() {
        super.receiveIntentExtras();
        mLabel = getIntent().getStringExtra("label");
        mInfoCode = getIntent().getStringExtra("infoCode");
    }

    @Override
    protected void initViews() {
        super.initViews();
        mPresenter.getByConditionByPublishType(mInfoCode);
    }

    @Override
    public String getTitleText() {
        return mLabel;
    }

    @Override
    protected InfomationListPresenter createPresenter() {
        return new InfomationListPresenter();
    }

    @Override
    public void getInfomationListSuccess(InfomationListBean response) {
        if (response.getData() != null) {
            loadinglayout.showContent();
            if (response.getData().getList() != null) {
                int total = response.getData().getTotal();
                int pageNum = response.getData().getPageNum();
                int pageSize = response.getData().getPageSize();
                boolean nextPage = false;
                if (total != 0) {
                    // 优先计算总页数
                    int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
                    //下一页等于当前页+1,但不能超过总页数界限
                    nextPage = pageNum + 1 <= totalPage ? true : false;
                }
                loadSuccess(nextPage, response.getData().getList());
            } else {
                loadinglayout.showEmpty();
            }
        } else {
            loadinglayout.showError();
        }
    }

    @Override
    public void getInfomationListFailed() {

    }

    @Override
    public void getByConditionByPublishTypeSuccess(ByPublishTypeBean response) {
        StringBuffer stringBuffer = new StringBuffer();
        if (response.getData() != null) {
            if (response.getData().size() > 0) {
                for (ByPublishTypeBean.DataBean bean : response.getData()) {
                    stringBuffer.append(bean.getGuid() + ",");
                }

                informationId = stringBuffer.substring(0, stringBuffer.length() - 1);
                refresh(false);
            }
        }
    }

    @Override
    public void getByConditionByPublishTypeFailed() {

    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mInfomationListAdapter = new InfomationListAdapter(mContext);
        mInfomationListAdapter.setOnItemClickListener(new InfomationListAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(int position, InfomationListBean.DataBean.ListBean bean) {
                //交易机构基本信息 jyjgJbxx
                if ("jyjgJbxx".equals(mInfoCode)) {
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_TRADINGINSTITUTIONBASICINFO)
                            .navigation();
                } else {
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_INFOMATIONCONTENT)
                            .withString("title", bean.getTitle())
                            .withString("operateDate", bean.getOperateDate())
                            .withString("publishType", bean.getPublishType())
                            .withString("content", bean.getContent())
                            .withString("attachment", bean.getAttachment())
                            .withString("readAmount", bean.getReadAmount() + "")
                            .navigation();
                }
            }
        });
        return mInfomationListAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        if (!TextUtils.isEmpty(informationId)) {
            mPresenter.getData(informationId, listSize, isloading);
        }
    }
}
